<?php

namespace App\Http\Controllers\Admin;

use App\Events\OrderEvent;
use App\Models\PayUnderline;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PayUderlineController extends BaseController
{
    public function index()
    {

        $items = PayUnderline::with('user')->where('status', 0)->orderByDesc('id')->paginate(20);

        return $this->view('admin.trade.pay_underline', [
            'items'=>$items,
            'pagination'=>$items->render()
        ]);
    }

    public function delete(Request $request)
    {
        $delete = $request->input('delete');
        if ($delete)
        {
            PayUnderline::whereIn('id', $delete)->delete();
        }
        return ajaxReturn();
    }

    public function accept(Request $request)
    {
        $id = $request->post('id');
        $status = $request->post('status');

        $pay = PayUnderline::find($id);
        $pay->status = $status;
        $pay->save();

        if ($status == 1)
        {
            $order = $pay->order;
            if ($order)
            {
                $order->changeToPaid();
                $order->trade->delete();
                $order->pay_type = 3;
                $order->save();

                event(new OrderEvent($order, 'paid'));
            }

        }

        return ajaxReturn();
    }
}
